package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.NearbyEntivity;
import com.sam.im.heiqun.uis.activities.FriendDetailActivity;
import com.sam.im.heiqun.uis.adapters.holder.NearbyHolder;
import com.sam.im.heiqun.utils.ToolsUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.sam.im.heiqun.utils.StringUtils;

/**
 * Created by leijiawen on 2017/6/15.
 */
public class NearbyAdapter extends BaseRecycleViewAdapter{
    private Context context ;
    private LayoutInflater inflater ;
    private List<NearbyEntivity.ListBean> nearbyEntivities = new ArrayList<>();

    public NearbyAdapter(Context context, List<NearbyEntivity.ListBean> nearbyEntivities) {
        this.context = context;
        this.nearbyEntivities = nearbyEntivities;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null ;
        View view = inflater.inflate(R.layout.layout_nearby_item,parent,false);
        return new NearbyHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NearbyEntivity.ListBean nearby = nearbyEntivities.get(position);
        NearbyHolder nearholder = (NearbyHolder) holder;
        final String id = nearby.getId();
        String headUrl = nearby.getHeadUrl();
        String name = nearby.getName();
        nearholder.tvName.setText(name);
        if (nearby.getSign()!=null&&!StringUtils.isEmpty(nearby.getSign())){
            nearholder.tvSign.setText(nearby.getSign()+"");
            nearholder.tvSign.setVisibility(View.VISIBLE);
        }else {
            nearholder.tvSign.setVisibility(View.GONE
            );
        }
        nearholder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, FriendDetailActivity.class);
                intent.putExtra("id",Long.parseLong(id));
                context.startActivity(intent);
            }
        });
        String latitude = nearby.getLatitude();
        String longitude = nearby.getLongitude();
        Double lat = Double.valueOf(latitude);
        Double lnt = Double.valueOf(longitude);
        String latitude1 = ToolsUtils.getSf(context, "lat");
        String longitude1 = ToolsUtils.getSf(context, "lnt");
        Double lat1 = Double.valueOf(latitude1);
        Double lnt1 = Double.valueOf(longitude1);
        double distance = ToolsUtils.getDistance(lnt, lat, lnt1, lat1);
        if (distance>=1000){
            double v = distance / 1000;
            DecimalFormat df   = new DecimalFormat("######0.00");
            String format = df.format(v);
            nearholder.tvLoction.setText(context.getResources().getString(R.string.juli_you)+format+context.getResources().getString(R.string.gongli));
        }else {
            int in = (int) distance;
            nearholder.tvLoction.setText(context.getResources().getString(R.string.juli_you)+in+context.getResources().getString(R.string.mi));
        }
        String sex = nearby.getSex();
        if (position==nearbyEntivities.size()-1){
            nearholder.view.setVisibility(View.GONE);
        }else {
            nearholder.view.setVisibility(View.VISIBLE);
        }

        if (!StringUtils.isEmpty(sex)){
            nearholder.imgSex.setVisibility(View.VISIBLE);
            switch (sex){
                case "男":
                    nearholder.imgSex.setImageResource(R.drawable.manimg);
                    break;
                case "女":
                    nearholder.imgSex.setImageResource(R.drawable.womenimg);
                    break;
            }
        }else {
            nearholder.imgSex.setImageResource(R.drawable.womenimg);
            nearholder.imgSex.setVisibility(View.VISIBLE);
        }
        Glide.with(context).load(headUrl).error(R.drawable.head_defaul).centerCrop().into(nearholder.imgIcon);
    }
    @Override
    public int getItemCount() {
        return nearbyEntivities.size();
    }


}
